package com.taotaojs.mybootsuper.mapper;

import com.taotaojs.mybootsuper.bean.BaseBean;
import com.taotaojs.mybootsuper.bean.KeyValueBean;
import com.taotaojs.mybootsuper.bean.KeyValueList;
import lombok.*;

import java.util.Collection;
import java.util.List;

/**
 * sql包装类
 * **仅对于单表对象的处理**
 *
 *
 * @author: TaoTaojs
 * @Date: 2020/4/21 3:17 PM
 * @Description:
 * @关联类:
 * @参考链接：
 * @History:
 * <author>        <time>                      <version>          <desc>
 * TaoTaojs        2020/4/21 3:17 PM                V1.0          自定义sql关键字封装类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@Builder
@EqualsAndHashCode(callSuper = false)
public class MySqlBuild<T extends BaseBean> {

    public static final String DESC = "DESC";

    private String select;
    private String from;
    private Join join;

    /**
     * where字符串-逻辑运算符
     */
    private List<Where> dataWhere;
    /**
     * 列名称-正反序
     */
    private KeyValueList<String, String> order;
    private String group;

    /**
     * 根据数据列表查询数据
     */
    private KeyValueList<String, Collection<?>> idsMapList;

    /**
     * 批量操作列表
     */
    private List<T> list;
    /**
     * 批量删除列表
     */
    private List<Integer> deleteList;


    /**
     * 默认查询所有
     * @param ids
     */
    public void setIdsMap(List<Integer> ids){
        if(idsMapList == null) {
            idsMapList = new KeyValueList<>();
        }
        idsMapList.add(KeyValueBean.create("id", ids));
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString(callSuper = true)
    @Builder
    @EqualsAndHashCode(callSuper = false)
    private static class Join{
        private List<String> left;
        private List<String> right;
        private List<String> inner;
    }

    @Data
    @AllArgsConstructor
    @ToString(callSuper = true)
    @Builder
    @EqualsAndHashCode(callSuper = false)
    public static class Where {
        private String whereStr;
        @Builder.Default
        private Connection connection = Connection.AND;

        Where(){
            connection = Connection.AND;
        }

    }

    /**
     * 逻辑运算符
     */
    public enum Connection{
        /**
         * not
         */
        NOT(" not "),
        /**
         * and
         */
        AND(" and "),
        /**
         * or
         */
        OR(" or ");

        /**
         * 唯一标识符
         */
        private String code;

        Connection(String code){
            this.code = code;
        }

    }



}
